# -*- coding: utf-8 -*-
# created on 2016/9/23
from mathsolver.functions.zhixian.Symmetry import PointSymmetryPoint
from mathsolver.functions.yuan.property import YuanJudge, GetYuanCenterPoint, ErYuanErCiCoeff
from mathsolver.functions.base import *
from mathsolver.functions.zhixian.property import GetLiangDianJuLi, GetXieLv001
from mathsolver.functions.zhixian.length import DianToXianJuLi
from sympy.abc import x, y, k
from mathsolver.functions.root.jiefangchen import JieFangChen
from mathsolver.functions.hanshu.dingyiyu import QiuDingYiYu
from mathsolver.functions.zhixian.zhixianqiexian import QuXianDaoShu


# 数形结合_求最值
class ValueRange001(BaseFunction):
    def solver(self, *args):
        eq = args[0].sympify()
        poly = args[1].sympify()
        if poly.is_Pow and poly.args[1].p == 1 and poly.args[1].q == 2:
            if YuanJudge(poly.args[0]):
                coeff = ErYuanErCiCoeff().solver(BaseEq([poly.args[0], S.Zero])).output[0]
                if coeff[0] == 1:
                    yuanxin = GetYuanCenterPoint().solver(BaseEq([poly.args[0], S.Zero])).output[0].value
                    self.steps.append(["", "依题意，得"])
                    self.steps.append(["", "%s的最小值即为:" % (new_latex(poly))])
                    self.steps.append(["", "(%s,%s)到直线%s=%s的距离" % (new_latex(yuanxin[0]), new_latex(yuanxin[1]), new_latex(eq[0]), new_latex(eq[1]))])
                    stepsolver = DianToXianJuLi().solver(BasePoint({"name": "", "value": yuanxin}), BaseEq(eq))
                    juli = stepsolver.output[0].value
                    self.steps += stepsolver.steps
                    self.steps.append(["", "%s的最小值为%s" % (new_latex(poly), new_latex(juli))])
                    self.output.append(BaseValue(juli))
        if YuanJudge(poly):
            coeff = ErYuanErCiCoeff().solver(BaseEq([poly, S.Zero])).output[0]
            if coeff[0] == 1:
                yuanxin = GetYuanCenterPoint().solver(BaseEq([poly, S.Zero])).output[0].value
                self.steps.append(["", "依题意，得"])
                self.steps.append(["", "%s的最小值即为:" % (new_latex(poly))])
                self.steps.append(["", "(%s,%s)到直线%s=%s的距离的平方" % (new_latex(yuanxin[0]), new_latex(yuanxin[1]), new_latex(eq[0]), new_latex(eq[1]))])
                stepsolver = DianToXianJuLi().solver(BasePoint({"name": "", "value": yuanxin}), BaseEq(eq))
                juli = stepsolver.output[0].value
                self.steps += stepsolver.steps
                self.steps.append(["", "%s的最小值为%s" % (new_latex(poly), new_latex(juli ** 2))])
                self.output.append(BaseValue(juli))
        self.label.add("数形结合_求最值")
        return self


# 曲线上的点到直线的最短距离
class ZuiZhi008(BaseFunction):
    def solver(self, *args):
        eq1 = args[0].sympify()
        # 求eq1的定义域
        f = (eq1[0] - eq1[1]).expand().simplify()
        symbol = default_symbol(f)
        a, h = f.as_independent(symbol)
        expr = - h
        dingyiyu = QiuDingYiYu().solver(expr, symbol)
        eq2 = args[1].sympify()
        quxian = eq1[0] - eq1[1]
        daoshu = QuXianDaoShu().solver(BaseEq(eq1)).output[0].value
        a, h = quxian.as_independent(y)
        left = h
        right = -(quxian - left)
        expression = right / left.coeff(y)
        self.steps.append(["", "∵ %s = %s" % (new_latex(eq1[0]), new_latex(eq1[1]))])
        self.steps.append(["", "∴ %s’ = %s" % (new_latex(daoshu[0]), new_latex(daoshu[1]))])
        xielv = GetXieLv001().solver(BaseEq(eq2)).output[0].value
        self.steps.append(["", "直线 %s = %s 的斜率 %s = %s" % (new_latex(eq2[0]), new_latex(eq2[1]), new_latex(k), new_latex(xielv))])
        self.steps.append(["", "由%s = %s,得" % (new_latex(daoshu[1]), new_latex(xielv))])
        solutions = JieFangChen().solver(BaseEq([daoshu[1], xielv])).output[0].value
        jies = solutions[solutions.keys()[0]]
        jie = jies & dingyiyu
        if len(jies.args) == 1:
            self.steps.append(["", self.output_jie(jies, x)])
        else:
            self.steps.append(["", self.output_jie(jies, x)])
            self.steps.append(["", "∵ %s %s %s" % (new_latex(symbol), '\u2208', new_latex(dingyiyu))])
            self.steps.append(["", "∴ %s = %s " % (new_latex(symbol), new_latex(jie.args[0]))])
        qiedian_x = jie.args[0]
        qiedian_y = expression.subs({x: qiedian_x})
        self.steps.append(["", "∴ 曲线%s = %s上的点(%s, %s)到直线 %s = %s 的距离最短" % (new_latex(eq1[0]), new_latex(eq1[1]), new_latex(qiedian_x), new_latex(qiedian_y), new_latex(eq2[0]), new_latex(eq2[1]))])
        stepsolver = DianToXianJuLi(verbose=True).solver(BasePoint({"name": "", "value": [qiedian_x, qiedian_y]}), BaseEq(eq2))
        self.steps += stepsolver.steps
        self.output.append(BaseValue(stepsolver.output[0].value))
        self.label.add("曲线上的点到直线的最短距离")
        return self


class ZuiZhi009(BaseFunction):
    def solver(self, *args):
        if len(args) == 4:
            points = args[0].sympify()
            point1 = points[0]
            point2 = points[1]
            target_poly = args[3].sympify()
        stepsolver = PointSymmetryPoint(verbose=True).solver(BasePoint(point1), args[1])
        self.steps += stepsolver.steps
        self.label.update(stepsolver.label)
        duichengdian = stepsolver.output[0].value
        juli = GetLiangDianJuLi().solver(BasePoint(point2), BasePoint(duichengdian)).output[0].value
        self.steps.append(["", "∴ %s 的最小值为 %s" % (new_latex(target_poly), new_latex(juli))])
        return self


if __name__ == '__main__':
    pass
